package com.zlp.mall.portal.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlp.mall.common.api.RespCode;
import com.zlp.mall.common.exception.CustomException;
import com.zlp.mall.common.util.BeanToUtils;
import com.zlp.mall.common.util.MethodUtil;
import com.zlp.mall.portal.modules.pms.dto.resp.ProductDetailResp;
import com.zlp.mall.portal.modules.pms.mapper.ProductMapper;
import com.zlp.mall.portal.modules.pms.model.Product;
import com.zlp.mall.portal.modules.pms.model.ProductAttributeValue;
import com.zlp.mall.portal.modules.pms.model.SkuStock;
import com.zlp.mall.portal.modules.pms.service.ProductAttributeService;
import com.zlp.mall.portal.modules.pms.service.ProductAttributeValueService;
import com.zlp.mall.portal.modules.pms.service.ProductService;
import com.zlp.mall.portal.modules.pms.service.SkuStockService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-10-13
 */
@Service
@RequiredArgsConstructor
@Slf4j(topic = "ProductServiceImpl")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final SkuStockService skuStockService;
    private final ProductAttributeValueService productAttributeValueService;
    private final ProductAttributeService productAttributeService;

    @Override
    public ProductDetailResp getProductDetail(Long productId) {

        log.info("getProductDetail.req productId={}",productId);
        Product product = this.getProductById(productId);
        ProductDetailResp productResp = BeanToUtils.doToDto(product, ProductDetailResp.class);
        // 商品 SKU
        List<SkuStock> skuStockList = skuStockService.getByProductId(productId);
        productResp.setSkuStockList(skuStockList);
        // 商品属性值
        List<ProductAttributeValue> productAttributeValueList = productAttributeValueService.getByProductId(productId);
        if (CollectionUtil.isNotEmpty(productAttributeValueList)) {
            for (ProductAttributeValue productAttributeValue : productAttributeValueList) {
                String attributeName = productAttributeService.getByAttrValueId(productAttributeValue.getProductAttributeId());
                productAttributeValue.setAttrName(attributeName);
            }
            productResp.setProductAttributeValueList(productAttributeValueList);
        }

        return productResp;
    }

    @Override
    public Product getProductById(Long productId) {

        log.info("getProductById.req productId={}",productId);
        Product product = this.getById(productId);
        if (Objects.isNull(product)) {
            throw new CustomException(RespCode.FRONT_PMS_3000, MethodUtil.getLineInfo());
        }
        return product;
    }

    @Override
    public List<Product> getByCategoryId(Long categoryId) {

        log.info("getByCategoryId.req categoryId={}",categoryId);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getProductCategoryId,categoryId);
        queryWrapper.eq(Product::getRecommandStatus,1);
        queryWrapper.orderByAsc(Product::getSort);
        return this.list(queryWrapper);
    }
}
